package com.example.test19_3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private OrderedBroadcastReceiver orderedBroadcastReceiver;
    private OrderedBroadcastReceiver2 orderedBroadcastReceiver2;
    private OrderedBroadcastReceiver3 orderedBroadcastReceiver3;
    private OrderedBroadcastReceiver4 orderedBroadcastReceiver4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction("com.example.test19_3.MY_OrderBROADCAST");
                sendOrderedBroadcast(intent,null);
            }
        });
        //在配置清单中静态注册广播接受者，会使接受者无法接收广播
        orderedBroadcastReceiver = new OrderedBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.test19_3.MY_OrderBROADCAST");
        intentFilter.setPriority(88);
        registerReceiver(orderedBroadcastReceiver,intentFilter);

        //将orderedBroadcastReceiver2的优先级设置的比orderedBroadcastReceiver高，则orderedBroadcastReceiver2先响应
        orderedBroadcastReceiver2 = new OrderedBroadcastReceiver2();
        IntentFilter intentFilter2 = new IntentFilter();
        intentFilter.addAction("com.example.test19_3.MY_OrderBROADCAST");
        intentFilter.setPriority(100);
        registerReceiver(orderedBroadcastReceiver2,intentFilter);

        //在orderedBroadcastReceiver3中截断，会使4无法接收
        orderedBroadcastReceiver3 = new OrderedBroadcastReceiver3();
        IntentFilter intentFilter3 = new IntentFilter();
        intentFilter.addAction("com.example.test19_3.MY_OrderBROADCAST");
        registerReceiver(orderedBroadcastReceiver3,intentFilter);

        orderedBroadcastReceiver4 = new OrderedBroadcastReceiver4();
        IntentFilter intentFilter4 = new IntentFilter();
        intentFilter.addAction("com.example.test19_3.MY_OrderBROADCAST");
        registerReceiver(orderedBroadcastReceiver4,intentFilter);

    }
}